package cn.itcast.service.impl;

import cn.itcast.dao.MenuDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Menu;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Stream;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    //查询菜单列表
    @Override
    public PageResult querymenuPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> menuPage = menuDao.querymenuPage();

        return new PageResult(menuPage.getTotal(), menuPage.getResult());
    }

    /**
     * 根据菜单id进行查询
     *
     * @param id
     * @return
     */
    @Override
    public Menu findById(Integer id) {

        return menuDao.findById(id);
    }

    /**
     * 新增主菜单
     *
     * @param menu
     * @param permissionIds
     * @return
     */
    @Transactional
    @Override
    public void addMenu(Menu menu, Integer[] permissionIds) {
        menuDao.addMenu(menu);
        //关联保存权限信息
        if (permissionIds != null && permissionIds.length > 0) {
            Integer id = menu.getId();
            setMenuAndPermission(id, permissionIds);
        }

    }


    /**
     * 设置菜单关联的权限
     *
     * @param menuId
     * @param permissionIds
     */
    private void setMenuAndPermission(Integer menuId, Integer[] permissionIds) {
        Stream.of(permissionIds).forEach(permissionId -> this.menuDao.setPermissionAssociation(menuId, permissionId));
    }

    /**
     * 编辑菜单
     *
     * @param menu
     * @param permissionIds
     */
    @Transactional
    @Override
    public void updateMenu(Menu menu, Integer[] permissionIds) {
        menuDao.updateMenu(menu);
        if (permissionIds != null && permissionIds.length > 0) {
            Integer menuId = menu.getId();
            //先删除编辑前权限关系
            int result = deletePermissionMenu(menuId);
            //添加新关系
            setMenuAndPermission(menuId, permissionIds);
        }
    }

    /**
     * 删除菜单权限关系
     *
     * @param menuId
     * @return
     */
    private int deletePermissionMenu(Integer menuId) {

        return menuDao.deletePermissionMenu(menuId);
    }

    /**
     * 删除菜单
     *
     * @param id
     */
    @Override
    public void deleteMenu(Integer id) {
        //先删除子菜单
        menuDao.deleteByPid(id);
        //再删除菜单
        menuDao.delete(id);
    }

    /**
     * 删除父菜单
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        menuDao.delete(id);
    }

    /**
     * 根据父菜单id删除子菜单id
     *
     * @param id
     */
    @Override
    public void deleteByPid(Integer id) {
        menuDao.deleteByPid(id);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Menu> findAll() {

        return menuDao.findAll();
    }
}
